package com.example.message2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SendMessage extends Activity {
	private EditText messages;
	private EditText sender;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_message);

		final EditText receiver = (EditText) findViewById(R.id.editText1);

		final EditText message = (EditText) findViewById(R.id.editText2);



		findViewById(R.id.button1).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						String sender = receiver.getText().toString();
						String messages = message.getText().toString();

						SharedPreferences settings = getSharedPreferences("preferences", 0);
						int watevs=settings.getInt("curUser", -1);
						String pwd=settings.getString("curPassword", "");
						if(pwd==""){
							Context context = getApplicationContext();
							Toast toast = Toast.makeText(context,
									pwd, Toast.LENGTH_LONG);
							toast.show();
						}
						HttpClient httpclient = new DefaultHttpClient();
						HttpPost httppost = new HttpPost(
								"http://softeng.cs.uwosh.edu/students/cs342g2/php/send.php");
						try { // Add your data
							List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
							nameValuePairs.add(new BasicNameValuePair("sender", watevs+""));
							nameValuePairs.add(new BasicNameValuePair("receiver", sender));
							nameValuePairs.add(new BasicNameValuePair("message", messages));
							nameValuePairs.add(new BasicNameValuePair("password", pwd));
							// nameValuePairs.add(new BasicNameValuePair(
							// "confpassword", password));
							httppost.setEntity(new UrlEncodedFormEntity(
									nameValuePairs));
							// Execute HTTP Post Request
							HttpResponse response = httpclient
									.execute(httppost);
							String result = EntityUtils.toString(response
									.getEntity());
							if (!result.equals("Success")) {
								Context context = getApplicationContext();
								Toast toast = Toast.makeText(context,
										result, Toast.LENGTH_LONG);
								toast.show();
								 close();
								


							} else {
								// new update().execute(user);
								// changeUser(user);
							}

						} catch (ClientProtocolException e) { // TODO
							// Auto-generated
							// catch
							// block

						} catch (IOException e) { // TODO Auto-generated
							// catch
							// block } }
						}



					}
				});
	}

	public void close()
	{
		this.finish();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.send_message, menu);
		return true;
	}

}
